package com.zq.assistant.func.ioc.finder;

import android.app.Activity;
import android.app.Fragment;
import android.view.View;

import com.zq.assistant.func.ioc.finder.cache.StrongCache;
import com.zq.assistant.func.ioc.finder.field.CachedFieldFinder;
import com.zq.assistant.func.ioc.finder.field.InjectFieldFinder;
import com.zq.assistant.func.ioc.finder.method.CachedMethodFinder;
import com.zq.assistant.func.ioc.finder.method.InjectMethodFinder;
import com.zq.assistant.func.ioc.finder.view.ActivityViewFinder;
import com.zq.assistant.func.ioc.finder.view.CachedViewFinder;
import com.zq.assistant.func.ioc.finder.view.ViewFinder;
import com.zq.assistant.func.ioc.finder.view.ViewViewFinder;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by zhangqiang on 17-7-11.
 */

public class FinderFactoryIMPL implements FinderFactory {

    private StrongCache<Integer,View> findViewCache = new StrongCache<>();
    private StrongCache<Class,List<Field>> findFieldCache = new StrongCache<>();
    private StrongCache<Class,List<Method>> findMethodCache = new StrongCache<>();

    @Override
    public ViewFinder getViewFinder(Object target) {

        ViewFinder viewFinder;

        if(target instanceof Activity){
            viewFinder = new ActivityViewFinder((Activity) target);
        }else if(target instanceof View){
            viewFinder = new ViewViewFinder((View) target);
        }else if(target instanceof ViewFinder){
            viewFinder = (ViewFinder) target;
        }else{

            throw new IllegalArgumentException("" + target.getClass());
        }
        return new CachedViewFinder(findViewCache, viewFinder);
    }

    @Override
    public InjectMethodFinder getMethodFinder(Object target) {

        if(target instanceof Activity){
            return new CachedMethodFinder(Activity.class,findMethodCache);
        }else if(target instanceof Fragment){
            return new CachedMethodFinder(Fragment.class,findMethodCache);
        }else if(target instanceof android.support.v4.app.Fragment){
            return new CachedMethodFinder(android.support.v4.app.Fragment.class,findMethodCache);
        }
        return new CachedMethodFinder(Object.class,findMethodCache);
    }

    @Override
    public InjectFieldFinder getFieldFinder(Object target) {

        if(target instanceof Activity){
            return new CachedFieldFinder(Activity.class,findFieldCache);
        }else if(target instanceof Fragment){
            return new CachedFieldFinder(Fragment.class,findFieldCache);
        }else if(target instanceof android.support.v4.app.Fragment){
            return new CachedFieldFinder(android.support.v4.app.Fragment.class,findFieldCache);
        }
        return new CachedFieldFinder(Object.class,findFieldCache);
    }
}
